﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using finProj.Entities;

namespace finProj.Gui
{
    public partial class clustersEdit : Form
    {
        private bool dragging = false;
        private List<Cluster> editedClusters;
        private Picture draggedPic;
        private int oldClusterId, pictureId;
        private bool edited = false;
        private int newClusterId;
        private int maxClusters;

        private Form3 form;

        public clustersEdit()
        {
            InitializeComponent();
            loadPictures();

        }
        public clustersEdit(Form3 form3)
        {
            this.form = form3;
            InitializeComponent();
            loadPictures();
        }

        private void loadPictures()
        {
            editedClusters = new List<Cluster>();
            editedClusters = Settings.Instance.clusterManager.clusters.GetRange(0, Settings.Instance.clusterManager.clusters.Count);
            maxClusters = Settings.Instance.clusterManager.clusters.Count;
            Drawform();

        }

        private void Drawform()
        {
            this.panel1.Dispose();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(600, 321);
            this.panel1.TabIndex = 0;


            int maxImages;
            int x = 3;
            int nexty = 20;
            for (int i = 0; i < maxClusters; i++)
            {
                maxImages = editedClusters[i].pictures.Count;
                this.panel2 = new System.Windows.Forms.Panel();
                this.label1 = new System.Windows.Forms.Label();
                // 
                // panel2
                // 
                this.panel2.BackColor = System.Drawing.SystemColors.InactiveBorder;
                this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.panel2.Location = new System.Drawing.Point(x, nexty);
                this.label1.Location = new System.Drawing.Point(x, nexty - 15);
                this.panel2.Name = "panel" + i;
                this.panel2.AllowDrop = true;
                panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
                panel2.DragEnter += new DragEventHandler(panel2_DragEnter);

                if (maxImages <= 3)
                {
                    this.panel2.Size = new System.Drawing.Size(550, 160);
                }
                else
                {
                    this.panel2.Size = new System.Drawing.Size(550, 160 * (int)(maxImages / 3));
                }

                nexty += this.panel2.Size.Height + 20;
                this.panel2.TabIndex = 0;
                this.panel2.TabStop = false;


                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Name = "label" + i;
                this.label1.Size = new System.Drawing.Size(35, 13);
                this.label1.TabIndex = 0;
                if (Settings.Instance.clusterManager.clusters[i].Name != null)
                {
                    this.label1.Text = editedClusters[i].Name;
                }
                else
                {
                    this.label1.Text = "No name given yet!";
                }

                int y = 3;

                for (int j = 0; j < maxImages; j++)
                {

                    this.pictureBox1 = new System.Windows.Forms.PictureBox();


                    if (j != 0 && j % 3 == 0)
                    {
                        y += 160;
                    }
                    this.pictureBox1.Location = new System.Drawing.Point(10 + 160 * (j % 3), y);


                    this.pictureBox1.Name = "pictureBox" + i + "~picture" + j;
                    this.pictureBox1.Size = new System.Drawing.Size(150, 150);
                    this.pictureBox1.TabIndex = 0;
                    this.pictureBox1.TabStop = false;
                    pictureBox1.Image = editedClusters[i].pictures[j].origPicture.Resize(150, 150, INTER.CV_INTER_LINEAR).ToBitmap();
                    pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);


                    this.panel2.Controls.Add(this.pictureBox1);
                    this.panel2.AutoScroll = true;

                }

                this.panel1.Controls.Add(this.panel2);
                this.panel1.Controls.Add(this.label1);
                this.panel1.AutoScroll = true;
                this.Controls.Add(this.panel1);
                this.panel1.Refresh();
            }


            this.Refresh();
        }



        void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox picturebox = (PictureBox)sender;
            string[] names = picturebox.Name.Split('~');
            string clusterName = names[0];
            string pictureName = names[1];
            string ch = "";

            int diff = clusterName.Length - "pictureBox".Length;
            for (int i = diff; i > 0; i--)
            {
                ch += clusterName[clusterName.Length - i];
            }
            oldClusterId = int.Parse(ch);
            ch = "";

            diff = pictureName.Length - "picture".Length;
            for (int i = diff; i > 0; i--)
            {
                ch += pictureName[pictureName.Length - i];
            }
            pictureId = int.Parse(ch);

            draggedPic = editedClusters[oldClusterId].pictures[pictureId];
            this.pictureBox1.DoDragDrop(picturebox, DragDropEffects.Move);

        }

        void panel2_DragEnter(object sender, DragEventArgs e)
        {
            //var panel = (Panel) sender;

            dragging = true;
            e.Effect = DragDropEffects.Move;

            
        }

        void panel2_DragDrop(object sender, DragEventArgs e)
        {
            dragging = false;
            edited = true;

            var panel = (Panel) sender;
            int diff = panel.Name.Length - "panel".Length;
            string ch = "";

            for (int i = diff; i > 0; i--)
            {
                ch += panel.Name[panel.Name.Length - i];
            }

            newClusterId = int.Parse(ch);
            this.saveButton.Enabled = true;

            editedClusters[oldClusterId].pictures.RemoveAt(pictureId);
            editedClusters[newClusterId].pictures.Add(draggedPic);

            Drawform();
        }

        private void saveChanges()
        {
            for (int i = 0; i < Settings.Instance.clusterManager.clusters.Count; i++)
            {
                var kmeans = new KMeans();
                Settings.Instance.clusterManager.clusters[i]=kmeans.normalizeClusterKmeans(Settings.Instance.clusterManager.clusters[i]);
            }
            Settings.Instance.clusterManager.clusters.Clear();
            Settings.Instance.clusterManager.clusters = editedClusters.GetRange(0, editedClusters.Count);


            this.saveButton.Enabled = false;
        }


        void clustersEdit_Closed(object sender, System.EventArgs e)
        {
           if(edited)
           {
               DialogResult result1 = MessageBox.Show("You have some pending changes. \nWould you like to save the changes?",
                "Pending changes",
                MessageBoxButtons.YesNo);

               if (result1 == DialogResult.Yes)
               {
                   saveChanges();
                   if (this.form != null)
                   {
                        this.form.DrawPanel1();
                   }

                   this.Dispose();
               }
               else
               {
                   if (this.form != null)
                   {
                       this.form.DrawPanel1();
                   }

                   this.Dispose();
               }
           }




        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (edited)
            {
                saveChanges();
                if (this.form != null)
                {
                    this.form.DrawPanel1();
                }
            }
            edited = false;
        }



   
    }
}
